package logic

import (
	"context"
	"weee/models"

	"weee/userrpc/internal/svc"
	"weee/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MobileLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMobileLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileLoginLogic {
	return &MobileLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// MobileLogin todo: 手机号登录-注册
func (l *MobileLoginLogic) MobileLogin(in *userrpc.MobileLoginRequest) (*userrpc.MobileLoginResponse, error) {
	// todo: add your logic here and delete this line
	SendCode := "send_code:" + in.Mobile
	code := models.Redis.Get(context.Background(), SendCode).Val()
	if code != in.Code {
		return &userrpc.MobileLoginResponse{
			Code: 400,
			Msg:  "验证码错误",
		}, nil
	}
	user, _ := models.GetUserInfo(models.User3{
		Mobile: in.Mobile,
	})
	if user.ID == 0 {
		user.Mobile = in.Mobile
		_, err := models.CreateUser(user)
		if err != nil {
			return &userrpc.MobileLoginResponse{
				Code: 400,
				Msg:  "注册失败",
			}, nil
		}
		return &userrpc.MobileLoginResponse{
			Code: 200,
			Msg:  "注册成功，登录成功",
		}, nil
	}
	return &userrpc.MobileLoginResponse{
		Code: 200,
		Msg:  "登录成功",
	}, nil
}
